#include "Color.h"
#include "FactoryUtil.h"

Color::Color() {
	this->setRGB("255000000");
}

Color::Color(string rgb) {
	this->setRGB(rgb);
}

Color::Color(string r,string g,string b) {
	this->setRGB(r,g,b);
}

Color::~Color() {
}

int Color::getR(){
	return this->R;
}

int Color::getG(){
	return this->G;
}

int Color::getB(){
	return this->B;
}

void Color::setR(int R){
	this->R = R;
}

void Color::setG(int G){
	this->G = G;
}

void Color::setB(int B){
	this->B = B;
}

string Color::getRGB(){
	return this->rgb;
}

void Color::setRGB(string r,string g,string b){
	this->R = FactoryUtil::getInstance()->getAsInt(r.c_str());
	this->G = FactoryUtil::getInstance()->getAsInt(g.c_str());
	this->B = FactoryUtil::getInstance()->getAsInt(b.c_str());
	this->rgb.append(r).append(g).append(b);

	bool notANumber = !ValidationUtils::isNumeric(rgb);
	if(notANumber || R < 0 || R > 255 || G < 0 || G > 255 || B < 0 || B > 255 ) {
		throw InvalidColorException("El color no es valido, las componetes RGB deben estar comprendidas entre 0 y 255.");
	}
}

void Color::setRGB(string rgb){
	bool notANumber = !ValidationUtils::isNumeric(rgb);
	if(notANumber || rgb.length() != 9 ) {
		throw InvalidColorException("El color no es valido, las componetes RGB deben estar comprendidas entre 0 y 255.");
	}
	string r = "";
	string g = "";
	string b = "";

	r.insert(0,rgb,0,3);
	g.insert(0,rgb,3,3);
	b.insert(0,rgb,6,3);

	this->rgb = rgb;
	this->R = FactoryUtil::getInstance()->getAsInt(r.c_str());
	this->G = FactoryUtil::getInstance()->getAsInt(g.c_str());
	this->B = FactoryUtil::getInstance()->getAsInt(b.c_str());

	if( R < 0 || R > 255 || G < 0 || G > 255 || B < 0 || B > 255 ) {
		throw InvalidColorException("El color no es valido, las componetes RGB deben estar comprendidas entre 0 y 255.");
	}
	this->rgb.append(r).append(g).append(b);
}
